//显示登录名称
erpload();
//小铃铛上显示小圆点
showMessage();

//获取当前用户所有权限
getAllRoleByUserCode();
//页面定时刷新消息
setInterval(function () {
    showMessage();
},1000*60);

//采购订单审批通知，采用伪审批
 setInterval(function () {
     getProcessBySPR();
 },1000*60);

 //只有财务部才能执行此方法
 let departmentCode=user.departmentCode;
let userCode=user.code;
 if (userCode==="RKB004"){
     setInterval(function () {
         getPendingSupplierList();
     },1000*60);
     getPendingSupplierList();
 }

getProcessBySPR();


function erpload() {
    $("#username")[0].textContent=user.name;
}

function showMessage() {
    //有未读消息时显示小圆点
//$(".layui-badge-dot")[0].remove();//移除小圆点
//$(".layui-icon-notice").after("<span class='layui-badge-dot'></span>")//加上小圆点
    $.ajax({
        type : 'get',
        url:'message/showMessage',
        dataType:"json",
        async:true,
        success : function (da) {
            if(da.status==200 && da.data>0){
                if($(".layui-badge-dot").length==0){
                    $(".layui-icon-notice").after("<span class='layui-badge-dot'></span>")//加上小圆点
                }
                layer.msg("有新消息了！");
            }
        }
    });
}

//登录时加载字典信息
async function  initDictionary() {

//当前登录人所管理的员工
   // var manageRuls= await  getManageRule("CustomerSalesman","customer","operator");
    getManageRuleINIT("CustomerSalesman","customer","operator","manageRuls");

//从字典中获取信息
    //var djs=await  getdictionaryF(6,26);//客户等级
    getdictionaryINIT(6,26,"djs");
    getdictionaryINIT(6,35,"gysdj");//供应商等级
   // var fls =await  getdictionaryF(6,27);//客户类型
    getdictionaryINIT(6,27,"fls");
  //  var countrys =await  getdictionaryF(6,29);//国家/地区
    getdictionaryINIT(6,29,"countrys");
  //  var sourceOfCustomerss =await  getdictionaryF(1,3);//客户来源
    getdictionaryINIT(1,3,"sourceOfCustomerss");
  //  var businessTypes =await  getdictionaryF(6,33);//业务类型
    getdictionaryINIT(6,33,"businessTypes");
  //  var spstatuss =await  getdictionaryF(6,34);//审批状态
    getdictionaryINIT(6,34,"spstatuss")
    getdictionaryINIT(6,36,"gysfl")//供应商分类
    getdictionaryINIT( 5,17,"sourceOfCustomers")//付款类型
    getdictionaryINIT( 1,87,"attributesOfCustomers")//客户属性
    getCurrentDepartmentUser2();//userTree
    getCurrentDepartmentUserAll();//userAllTree
}
initDictionary();
//异步访问字典
function getdictionaryINIT(dl,xl,intArr) {

    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data: {"type":dl,"lx":xl},
        url:'dictionary2/getValues',
        dataType:"json",
        // contentType: 'application/json',
        async:true,
        success : function (da) {
            if(da.status==200 ){
                window.sessionStorage.setItem(intArr,JSON.stringify(da.data));
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });

}
//获取当前登录人的管理权限，即其所管理的员工,code:字典中所配置的管理者所对应的用户组编码,model:哪个模块,type:哪个类型，即是操作员还是业务员
function getManageRuleINIT(code,model,lx,initArr){


    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        data: {"code":code,model:model,lx:lx},
        url:'department/selectUserTree',
        dataType:"json",
        // contentType: 'application/json',
        async:true,
        success : function (da) {
            if(da.status==200 ){
                window.sessionStorage.setItem(initArr,JSON.stringify(da.data));
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });

}
function getCurrentDepartmentUser2() {
    $.ajax({
        type: 'get',
        url: 'department/getCurrentDepartmentUser',
        dataType: "json",
        async: false,
        success: function (da) {
            if (da.status == 200) {
                window.sessionStorage.setItem("userTree",JSON.stringify(da.data));
            }
        },
        error: function (data) {
        }
    });

}
//获取所有用户
function getCurrentDepartmentUserAll() {
    $.ajax({
        type: 'get',
        url: 'department/getAllDepartmentUser',
        dataType: "json",
        async: false,
        success: function (da) {
            if (da.status == 200) {
                window.sessionStorage.setItem("userAllTree",JSON.stringify(da.data));
            }
        },
        error: function (data) {
        }
    });

}
//获取当前用户所有权限
function  getAllRoleByUserCode() {

    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
       // data: {"type":dl,"lx":xl},
        url:'user/getAllRoleByUserCode',
        dataType:"json",
        // contentType: 'application/json',
        async:true,
        success : function (da) {
            if(da.status==200 ){
                window.sessionStorage.setItem("AllRole",JSON.stringify(da.data));
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
}
//采购订单审批通知
function getProcessBySPR(){

    $.ajax({
        type : 'get',
        url:'purchase/getProcessBySPR',
        dataType:"json",
        async:true,
        success : function (da) {

            if(da.status==200 ){
                var arrays=da.data;
                if(arrays.length>0){
                    var dd="";
                    for(var i=0;i<arrays.length;i++){
                        if(i == arrays.length-1){
                            dd=dd+arrays[i].purchaseNo;
                        }else{
                            dd=dd+arrays[i].purchaseNo+",";
                        }

                    }

                    //页面层
                    $("#getProcessBySPR")[0].innerText="您有新的采购订单待审批，请前往采购模块进行处理！\n订单号为："+dd;
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['420px', '240px'], //宽高
                        content: $("#getProcessBySPR"),
                        offset:"rb",
                        shade: 0,
                        time: 3000
                    });
                }


              // $("#getProcessBySPR");

            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });

}
//供应商审批通知
function getPendingSupplierList(){
    let supplierStr="";
    $.ajax({
        type : 'get',
        url:'customer2/getPendingSupplierList',
        dataType:"json",
        async:false,
        success : function (data) {
            if(data.status==200 ){
                let datas=data.data
                let names=[];
                let paymentAccountNos=[];
                let codes=[];
                let abbreviations=[];
                for (let i in datas){
                    if (datas[i].name !=""){
                        names.push(datas[i].name);
                    }
                    if (datas[i].paymentAccountNo !=""){
                        paymentAccountNos.push(datas[i].paymentAccountNo);
                    }
                    if (datas[i].code !=""){
                        codes.push(datas[i].code);
                    }
                    if (datas[i].abbreviation !=""){
                        abbreviations.push(datas[i].abbreviation);
                    }
                }

                let nameArray=names.join(", ");
                let paymentAccountNoArray=paymentAccountNos.join(", ");
                let codeArray=codes.join(", ");
                let abbreviationArray=abbreviations.join(", ");

                if(datas.length>0){
                    supplierStr="您有新的供应商待审批，请前往供应商模块进行处理！"+
                        "\n 供应商名称为："+nameArray+
                        "\n 供应商代码为："+paymentAccountNoArray+
                        "\n 供应商编码为："+codeArray+
                        "\n 供应商简称为："+abbreviationArray;
                    //页面层
                    // $("#getProcessBySPR")[0].innerText="您有新的供应商待审批，请前往供应商模块进行处理！"+str;
                    // layer.open({
                    //     type: 1,
                    //     skin: 'layui-layer-rim', //加上边框
                    //     area: ['420px', '240px'], //宽高
                    //     content: $("#getProcessBySPR"),
                    //     offset:"rb",
                    //     shade: 0,
                    //     time: 30000
                    // });
                }
            }
        }
    });


    //-------------------------------------
    //查询授信客户
    let customerStr="";
    $.ajax({
        type : 'get',
        url:'customer/getCreditCustomerList',
        dataType:"json",
        async:false,
        success : function (data) {
            if(data.status==200 ){
                let datas=data.data
                let names=[];
                let shortCodes=[];
                let codes=[];
                let abbreviations=[];
                for (let i in datas){
                    if (datas[i].name !=""){
                        names.push(datas[i].name);
                    }
                    if (datas[i].shortCode !=""){
                        shortCodes.push(datas[i].shortCode);
                    }
                    if (datas[i].code !=""){
                        codes.push(datas[i].code);
                    }
                    if (datas[i].abbreviation !=""){
                        abbreviations.push(datas[i].abbreviation);
                    }
                }

                let nameArray=names.join(", ");
                let shortCodeArray=shortCodes.join(", ");
                let codeArray=codes.join(", ");
                let abbreviationArray=abbreviations.join(", ");

                if(datas.length>0){
                    customerStr="您有新的客户授信，请前往客户模块进行处理！"+
                        "\n 客户名称为："+nameArray+
                        "\n 客户简码为："+shortCodeArray+
                        "\n 客户编码为："+codeArray+
                        "\n 客户简称为："+abbreviationArray;
                    //页面层
                    /*$("#getProcessBySPR")[0].innerText="您有新的客户授信，请前往客户模块进行处理！"+str;
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['420px', '240px'], //宽高
                        content: $("#getProcessBySPR"),
                        offset:"rb",
                        shade: 0,
                        time: 30000
                    });*/
                }
            }
        }
    });
    let allStr="";
    if (supplierStr.length>0){
        allStr+=supplierStr+"\n";
    }
    if (customerStr.length>0){
        allStr+=customerStr+"\n";
    }
    if (allStr.length>0){
        $("#getProcessBySPR")[0].innerText=allStr;
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['420px', '240px'], //宽高
            content: $("#getProcessBySPR"),
            offset:"rb",
            shade: 0,
            time: 30000
        });
    }

}
